<?php
 namespace app\redis\controller;
 use \think\Cookie;
 class Ms extends \think\Controller{
	 private $time;
	 private $redis;
	 function __construct(){
	     parent::__construct();
		 $this->time=strtotime('2019-12-3 14:14:59');
		 $this->redis=new \redis();
	     $this->redis->connect('localhost',6379);
	     $this->redis->auth('1234');
	  }
	  function start(){
	     for($i=1;$i<=5;$i++){
		    $this->redis->lpush('mssp',1);
		 }
	  }
      function index(){
		  $this->assign(['time'=>$this->time,'time2'=>time(),'num'=>$this->redis->lsize('mssp')]);
	     return $this->fetch('index/ms');
	  }


	  function qianggou(){
	    //判断是否登入
		if(!$user=Cookie::get('nameid')){
		    die('未登入！,<a href="/redis.php/friend/index">去登入</a>');
		}
		//判断时间是否到抢购时间
	   if(($this->time-time())>0){
	       die('还未到抢购时间！,<a href="javascript:history.back(-1);">返回</a>');
	   }
		//判断是否已抢购
          $data=$this->redis->hgetall('msuser:'.$user);
		  if($data){
		     die('你已抢购！,<a href="javascript:history.back(-1);">返回</a>');
		  }
		//数据存入
		 $this->redis->lpop('mssp');
		 $this->redis->lpush('userid',$user);
		 $this->redis->hmset('msuser:'.$user,['time'=>time(),'sp_id'=>1]);
	  }


	  function old_time(){
	     $user=$this->redis->lrange('userid',0,-1);
		 foreach($user as $v){
		     $res=$this->redis->hgetall('msuser:'.$v);
			 if((time()-$res['time'])>=60){
			     $this->redis->lpush('mssp',1);
				 $this->redis->del('msuser:'.$v);
			 }
		 }
	  }
 }
?>